package com.ikas.ai.server.module.assessment.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.server.error.BusinessException;
import com.ikas.ai.server.module.assessment.dao.AssessWfResultMapper;
import com.ikas.ai.server.module.assessment.enums.TriggerFromEnum;
import com.ikas.ai.server.module.assessment.model.dto.AwfResultAddDto;
import com.ikas.ai.server.module.assessment.model.dto.AwfResultOfWorkCdDto;
import com.ikas.ai.server.module.assessment.model.dto.AwfResultQueryDto;
import com.ikas.ai.server.module.assessment.model.po.AssessWfResult;
import com.ikas.ai.server.module.assessment.model.vo.AwfResultDetailCalVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 工况模型评价结果表 服务实现类
 * </p>
 *
 * @author wuyf
 * @since 2023-10-31
 */
@Service
public class AssessWfResultService extends ServiceImpl<AssessWfResultMapper, AssessWfResult> {

    @Resource
    private AssessWfResultDetailService assessWfResultDetailService;

    public List<AssessWfResult> listAwfResult(AwfResultQueryDto queryDto) {
        LambdaQueryWrapper<AssessWfResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(queryDto.getMachineNo()), AssessWfResult::getMachineNo, queryDto.getMachineNo());
        queryWrapper.eq(Objects.nonNull(queryDto.getModelId()), AssessWfResult::getModelId, queryDto.getModelId());
        queryWrapper.eq(StringUtils.isNotBlank(queryDto.getWfName()), AssessWfResult::getWfName, queryDto.getWfName());
        queryWrapper.eq(StringUtils.isNotBlank(queryDto.getMeteCode()), AssessWfResult::getMeteCode, queryDto.getMeteCode());
        queryWrapper.ge(StringUtils.isNotBlank(queryDto.getStartBatch()), AssessWfResult::getBatch, queryDto.getStartBatch());
        queryWrapper.le(StringUtils.isNotBlank(queryDto.getEndBatch()), AssessWfResult::getBatch, queryDto.getEndBatch());
        //默认只展示工况转换来源结果
        if (Objects.nonNull(queryDto.getTriggerFrom())) {
            queryWrapper.eq(AssessWfResult::getTriggerFrom, queryDto.getTriggerFrom());
        } else {
            queryWrapper.eq(AssessWfResult::getTriggerFrom, TriggerFromEnum.WORK_FLOW_TRANS.getValue());
        }
        queryWrapper.orderByDesc(AssessWfResult::getId);
        return this.list(queryWrapper);
    }

    public boolean addAwfResult(AwfResultAddDto addDto) {
        AssessWfResult awfResult = BeanUtil.copyProperties(addDto, AssessWfResult.class);
        return this.save(awfResult);
    }

    public JsonResult detailAwfResult(Long id) {
        return JsonResult.ok(assessWfResultDetailService.listByRid(id));
    }

    /**
     * 通过流程实例id获取评价结果值
     *
     * @param modelId        模型id
     * @param flowInstanceId 流程实例id
     * @return AssessWfResult
     */
    public AssessWfResult getAwfResultByFlowInstanceId(Long modelId, Long flowInstanceId) {
        LambdaQueryWrapper<AssessWfResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AssessWfResult::getModelId, modelId);
        queryWrapper.eq(AssessWfResult::getFlowInstanceId, flowInstanceId);
        return this.getOne(queryWrapper);
    }

    public Integer countOfResultDetailByMeteCode(String meteCode) {
        List<AssessWfResult> list = getListByMeteCode(meteCode);
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        AssessWfResult lastResult = list.get(0);
        return assessWfResultDetailService.countOfResultDetailByRid(lastResult.getId());
    }

    public List<AwfResultDetailCalVo> getResultDetailByMeteCode(String meteCode) {
        Assert.notBlank(meteCode, "测点编号不能为空");
        List<AssessWfResult> list = this.list(
                new LambdaQueryWrapper<AssessWfResult>().eq(AssessWfResult::getMeteCode, meteCode).orderByDesc(AssessWfResult::getCreateTime).last("limit 1")
        );
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException("测点编号没有对应结果");
        }
        return assessWfResultDetailService.getResultDetailCalByRid(list.get(0).getId());
    }

    public AssessWfResult detailOfWorkCd(AwfResultOfWorkCdDto workCdDto) {
        LambdaQueryWrapper<AssessWfResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AssessWfResult::getMachineNo, workCdDto.getMachineNo())
                .eq(AssessWfResult::getMeteCode, workCdDto.getMeteCode())
                .eq(AssessWfResult::getBatch, workCdDto.getBatch())
                .eq(AssessWfResult::getWfName, workCdDto.getWfName())
                .eq(AssessWfResult::getStepName, workCdDto.getStepName())
                .last("limit 1");
        return this.getOne(queryWrapper);
    }

    /**
     * 根据meteCode查询评价结果
     *
     * @param meteCode 测点编号
     * @return list of assessWfResult
     */
    private List<AssessWfResult> getListByMeteCode(String meteCode) {
        Assert.notBlank(meteCode, "测点编号不能为空");
        LambdaQueryWrapper<AssessWfResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AssessWfResult::getMeteCode, meteCode);
        queryWrapper.orderByDesc(AssessWfResult::getCreateTime);
        return this.list(queryWrapper);
    }

}
